package com.yiyiooo.Abp.utils;

import android.os.Environment;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class FileUtils {
    static String PATH = "";
    public static final String FILE_TYPE_FOLDER="wFl2d";
    public static final String FILE_INFO_NAME="fName"; // 文件名称
    public static final String FILE_INFO_ISFOLDER="fIsDir"; // 是否为文件夹
    public static final String FILE_INFO_TYPE="fFileType"; // 文件的后缀
    public static final String FILE_INFO_NUM_SONDIRS="fSonDirs"; // 子文件夹个数
    public static final String FILE_INFO_NUM_SONFILES="fSonFiles"; // 子文件个数
    public static final String FILE_INFO_PATH="fPath"; // 文件的绝对路径

    public static List<File> findFileGreTime(String path,Long time){
        File specItemDir = new File(path);
        List<File> flies = searchFiles(specItemDir);
        List<File> fsLs = new ArrayList<>();
        for (File item : flies) {
            String name = item.getName();
            long lastTime = time - item.lastModified();
            if(pname.matcher(name).find() && lastTime>-5000 && lastTime<5000){
                fsLs.add(item);
            }
        }
        return fsLs;
    }
    public static String findFileRegName(String Name,String path,Long time){
        File specItemDir = new File(path);
        List<File> flies = searchFiles(specItemDir);
        String regName = "";
        for (int i =0; i<Name.length();i++){
            regName+="[\\s-_]?";
            if(Name.charAt(i)=='+') regName += "\\+";
            if(Name.charAt(i)=='*') regName += "\\*";
            else regName+=Name.charAt(i);
        }
        Pattern pname=Pattern.compile(regName);
        for (File item : flies) {
            String name = item.getName();
            long lastTime = time - item.lastModified();
            if(pname.matcher(name).find() && lastTime>-5000 && lastTime<5000){
                return item.getPath();
            }
        }
        return "";
    }
    //    获取sd卡根目录
    public static String getSDPath(){
        String sdcard= Environment.getExternalStorageState();
        if(sdcard.equals(Environment.MEDIA_MOUNTED)){
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }else{
            return null;
        }
    }
    //  获取文件列表
    public static List<File> searchFiles(File folder) {
        List<File> result = new ArrayList<>();
        if (folder.isFile())
            result.add(folder);
        File[] subFolders = folder.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                if (file.isDirectory()&&!checkFileName(file.getName())) {
                    return true;
                }
                if (checkFileNameEnd(file.getName())) {
                    return true;
                }
                return false;
            }
        });
        if (subFolders != null) {
            for (File file : subFolders) {
                if (file.isFile()) {
                    // 如果是文件则将文件添加到结果列表中
                    result.add(file);
                } else {
                    // 如果是文件夹，则递归调用本方法，然后把所有的文件加到结果列表中
                    result.addAll(searchFiles(file));
                }
            }
        }
        return result;
    }

    //匹配目录规则
    static Pattern  pdir=Pattern.compile("Android|androidesk|baidu|backup|com.|DCIM|Documents|Download|tencent");
    private static boolean checkFileName(String name){
        if (pdir.matcher(name).find()){
            return true;
        }else {
            return  false;
        }
    }
    //匹配后缀规则
    static Pattern  pname=Pattern.compile("(wav|3gp|mp3|amr|aac|m4a)$");
    private static boolean checkFileNameEnd(String name) {
        if (pname.matcher(name).find()){
            return true;
        }else {
            return  false;
        }
    }
}


